package com.example.server.controller;

import com.example.server.bean.Admin;
import com.example.server.bean.FastDFSFile;
import com.example.server.service.IAdminService;
import com.example.server.util.FastDFSUtil;
import com.example.server.util.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * ProjectName: yeb
 * Author: 别怿言
 * Date: 2023-02-14 23:49
 * Description：个人中心接口
 */
@Api(tags = "AdminCenter")
@RestController
@RequestMapping("/admincenter")
public class AdminCenterController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "更新操作员信息")
    @PutMapping("/admin/info")
    public RespBean updateAdmin(@RequestBody Admin admin, Authentication authentication){
        if(adminService.updateById(admin)){
            SecurityContextHolder.getContext().setAuthentication(
                    new UsernamePasswordAuthenticationToken(admin,null,authentication.getAuthorities()));
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @ApiOperation(value = "更新密码")
    @PutMapping("/admin/pass")
    public RespBean updateAdminPass(@RequestBody Map<String,Object> info){
        String oldPass = (String) info.get("oldPass");
        String pass = (String) info.get("pass");
        Integer adminId = (Integer) info.get("adminId");
        return adminService.updateAdminPass(oldPass,pass,adminId);
    }

    @ApiOperation(value = "更新头像")
    @PostMapping("/admin/userface")
    public RespBean updateAdminUserface(MultipartFile file,Integer id,Authentication authentication) throws Exception {
        String[] filepath= FastDFSUtil.upload(file);
        String url= FastDFSUtil.getTrackerurl()+filepath[0]+"/"+filepath[1];
        return adminService.updateUserface(url,id,authentication);
    }


}
